﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS;
using System.IO;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;

namespace GISDeveloping_test2
{
    public partial class Form1 : Form
    {

        private ITOCControl2 m_tocControl;
        private IMapControl3 m_mapControl;
        private IToolbarMenu m_menuMap;
        private IToolbarMenu m_menuLayer;
        private bool isMouseWheelDown = false;
        private int lastMouseX;
        private int lastMouseY;
        public ILayer pMoveLayer;
        public int toIndex;

        public Form1()
        {
            InitializeComponent();
        }

        private void cmdLoadShp_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Shapefile(*.shp)|*.shp";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                string shapefile_path = openFileDialog1.FileName;
                try
                {
                    string shapefile_dir = System.IO.Path.GetDirectoryName(shapefile_path);
                    string shapefile_name = System.IO.Path.GetFileName(shapefile_path);
                    axMapControl1.AddShapeFile(shapefile_dir, shapefile_name);
                    MessageBox.Show(string.Format("加载Shapefile数据成功：\n{0}", shapefile_name), "加载成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(string.Format("加载Shapefile数据失败：\n{0}",shapefile_path), "加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

            
        }

        private void cmdClearLayers_Click(object sender, EventArgs e)
        {
            axMapControl1.ClearLayers();
            
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            //Click right button and open context menu
            if(MouseButtons==MouseButtons.Right){
            contextMenuStrip1.Show(MousePosition);
            }

        }

        private void cmdOpenXmd_Click(object sender, EventArgs e)
        {
            MapDocument mapDocument1 = new MapDocument();
            OpenFileDialog openFile1 = new OpenFileDialog();
            openFile1.Filter = "地图文档(*.mxd)|*.mxd";
            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                mapDocument1.OpenDocument(axMapControl1, openFile1.FileName);
            }
            

        }

        private void cmdSaveXmd_Click(object sender, EventArgs e)
        {
            MapDocument mapDocument2 = new MapDocument();
            SaveFileDialog saveFile1 = new SaveFileDialog();
            saveFile1.Filter = "地图文档(*.mxd)|*.mxd";
            if (saveFile1.ShowDialog() == DialogResult.OK)
            {
                mapDocument2.OpenDocument(axMapControl1, saveFile1.FileName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_tocControl = (ITOCControl2)axTOCControl1.Object;
            m_mapControl = (IMapControl3)axMapControl1.Object;
            m_menuLayer = new ToolbarMenuClass();
            m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ZoomToLayer(), -1, 0, true, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.SetHook(m_mapControl);
            axTOCControl1.LabelEdit = esriTOCControlEdit.esriTOCControlManual;//设置LabelEdit属性为手动模式
        }

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap map = null;
            ILayer layer = null;
            object other = null;
            object index = null;
            //TOCControl右键菜单
            if (e.button == 2)
            {
                //定义被选中的对象
                m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
                //确保对象被选中
                if (item == esriTOCControlItem.esriTOCControlItemMap)
                    m_tocControl.SelectItem(map, null);
                else
                    m_tocControl.SelectItem(layer, null);
                m_mapControl.CustomProperty = layer;
                m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
            }
            //TOCControl拖动图层
            if (e.button == 1)
            {
                m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
                if (item==esriTOCControlItem.esriTOCControlItemLayer)
                {
                    if (layer is IAnnotationSublayer)
                    {
                        return;//如果是注记图层则返回
                    }
                    else
                    {
                        pMoveLayer = layer;
                    }
                }
            }
        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            //按下鼠标滚轮
            if (e.button == 4)
            {
                isMouseWheelDown = true;
                lastMouseX = e.x;
                lastMouseY = e.y;
            }
        }

        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            // 如果按下了鼠标中键，更新地图显示范围
            if (e.button==4)
            {
                // 获取当前地图范围
                IEnvelope extent = axMapControl1.ActiveView.Extent;

                // 计算鼠标移动的距离
                double deltaX = axMapControl1.ToMapPoint(lastMouseX, lastMouseY).X - axMapControl1.ToMapPoint(e.x, e.y).X;
                double deltaY = axMapControl1.ToMapPoint(lastMouseX, lastMouseY).Y - axMapControl1.ToMapPoint(e.x, e.y).Y;

                // 更新地图显示范围
                extent.Offset(deltaX, deltaY);
                axMapControl1.ActiveView.Extent = extent;
                axMapControl1.ActiveView.Refresh();

                // 更新上一次鼠标位置
                lastMouseX = e.x;
                lastMouseY = e.y;
            }
        }

        private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
        {
            // 释放鼠标滚轮
            if (e.button == 4)
            {
                isMouseWheelDown = false;
            }
        }

        private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
        {
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap basicMap = null;
            ILayer layer = null;
            object other = null;
            object index = null;
            //移动图层顺序
            if (e.button == 1)
            {
                m_tocControl.HitTest(e.x, e.y, ref item, ref basicMap, ref layer, ref other, ref index);
                if (pMoveLayer != layer)//如果是原图层则不用操作
                {
                    IMap map = axMapControl1.Map;
                    ILayer tempLayer;
                    for (int i = 0; i < map.LayerCount; i++)
                    {
                        tempLayer = map.get_Layer(i);
                        if (tempLayer == layer)
                            toIndex = i;//获取移动后的图层索引
                    }
                    map.MoveLayer(pMoveLayer, toIndex);
                    axMapControl1.ActiveView.Refresh();
                    m_tocControl.Update();
                }
            }
        }


        //禁止将图层名修改为空
        private void axTOCControl1_OnEndLabelEdit(object sender, ITOCControlEvents_OnEndLabelEditEvent e)
        {
            if (e.newLabel.Trim()=="")
            {
                e.canEdit = false;
                MessageBox.Show("无法将图层名修改为空","非法的命名",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
    }
}



